package com.telecomyt.jwportal.activity.contacts.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.activity.contacts.bean.RegionInfo;

import java.util.List;

/**
 * @author created by chengDa
 * @time 2020/10/22  16:30
 * @description
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private Context context;
    private List<RegionInfo> datas;

    public MyAdapter(Context context, List<RegionInfo> list) {
        this.context = context;
        this.datas = list;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.activity_contacts_items, null);
        return new MyViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setIsRecyclable(false); //解决itemView重用的问题，禁止会降低效率
        RegionInfo regionInfo = datas.get(position);
        String code = regionInfo.getCode().substring(0, 1);
        if (position == 0) {
            holder.word.setText(code);
        } else {

            String preCode = datas.get(position - 1).getCode().substring(0, 1);
            if (preCode.equals(code)) {
                holder.word.setVisibility(View.GONE);
            } else {
                holder.word.setText(code);
            }
        }
        holder.mTvContractsPhone.setText(regionInfo.getPhone());
        holder.mTvContractsName.setText(regionInfo.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.itemClick(position);
            }
        });
        holder.mIvContractsCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.itemClickCall(position,regionInfo.getPhone());
            }
        });

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView mTvContractsName, mTvContractsPhone;
        public TextView word;
        private ImageView mIvContractsCall, mIvContractsMsg;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTvContractsName = (TextView) itemView.findViewById(R.id.mTvContractsName);
            mTvContractsPhone = itemView.findViewById(R.id.mTvContractsPhone);
            word = (TextView) itemView.findViewById(R.id.mTvCode);
            mIvContractsCall = (ImageView) itemView.findViewById(R.id.mIvContractsCall);
            mIvContractsMsg = (ImageView) itemView.findViewById(R.id.mIvContractsMsg);
        }
    }

    private MyItemClickListener mListener;

    public interface MyItemClickListener {
        void itemClick(int position);
        void itemClickCall(int position,String phone);
    }

    public void setOnRecyclerViewListener(MyItemClickListener onRecyclerViewListener) {
        this.mListener = onRecyclerViewListener;
    }

}